import { fileList } from '@/api/user'
const BenzAMRRecorded = require('benz-amr-recorder')

const oldState = {
  // 是否显示聊天详情
  ifShowChatDetail: false,
  moveMessage_id: '', // 正在移动的未读消息盒子id
  beginMove_messageNumber: false, // 消息数是否移动
  fileList: [],
  // 是否显示右键菜单(删除联系人）
  ifShowRightList: false,
  // 是否显示右键菜单（消息操作）
  ifShowMsgRightList: null,
  // 消息操作的右键菜单
  operateMenu: [
    {
      id: 0,
      name: '转发',
      types: ['words', 'file', 'pic'],
      icon: require('@/assets/message/forward.png'),
      fnName: 'forward'
    },
    {
      id: 1,
      name: '撤回',
      types: ['words', 'file', 'pic'],
      icon: require('@/assets/message/withdraw.png'),
      fnName: 'withdraw'
    },
    {
      id: 2,
      name: '下载',
      types: ['file', 'pic'],
      icon: ('@/assets/message/download.png'),
      fnName: 'download'
    },
    {
      id: 3,
      name: '收藏至文件',
      types: ['file'],
      icon: require('@/assets/message/collect.png'),
      fnName: 'collect'
    },
    {
      id: 4,
      name: '删除',
      types: ['words', 'pic', 'file', 'voice'],
      icon: require('@/assets/message/delete.png'),
      fnName: 'delete'
    }
  ],
  // 撤回的消息及对应用户
  withdrawInfo: {},
  // 语音文件承载的amr处理对象
  amr: null,
  // amr对象对应的msgID
  amrID: null,
  // 1对1 好友视频通话待接听相关数据
  videoCallInfo: {
    isInviter: null,
    oppositeID: null
  },
  // 当前视频通话msg_id
  videoCallMsgID: null
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  // 重置原始状态
  reset_state (state) {
    for (const key in state) {
      state[key] = oldState[key]
    }
  },
  // 设置是否显示聊天详情
  set_ifShowChatDetail (state, bool) {
    state.ifShowChatDetail = bool
  },
  // 设置移动的消息盒子id
  set_moveId (state, id) {
    state.moveMessage_id = id
  },
  // 是否开启消息数移动
  set_messageMove (state, flag) {
    state.beginMove_messageNumber = flag
  },
  // 设置文件列表
  set_fileList (state, list) {
    state.fileList = list
  },
  // 设置文件列表单个文件选中状态
  set_file_ifSelect (state, item) {
    state.fileList.forEach(part => {
      if (item.file_id === part.file_id) {
        part.ifSelect = !part.ifSelect
      }
    })
  },
  // 设置是否显示右键菜单(删除最近联系人）
  set_ifShowRightList (state, bool) {
    state.ifShowRightList = bool
  },
  // 设置消息右键菜单
  set_ifShowMsgRightList (state, msgId) {
    state.ifShowMsgRightList = msgId
  },
  // 设置撤回的消息及对象
  set_withdrawInfo (state, info) {
    state.withdrawInfo = info
  },
  // 设置amr对象
  set_amr (state, { blob, msgID }) {
    if (msgID === state.amrID && state.amr) {
      if (state.amr._isPlaying) {
        state.amr.pause()
      } else {
        state.amr.play()
      }
      return
    }
    if (state.amr) {
      state.amr.pause()
      state.amr = null
    }
    state.amr = new BenzAMRRecorded()
    state.amr.initWithBlob(blob).then(() => {
      state.amr.play()
      state.amr.onEnded(() => {
        state.amr = null
      })
    })
  },
  // 设置amr对象对应的msgID
  set_amrID (state, msgID) {
    state.amrID = msgID
  },
  // 设置1对1 好友视频相关数据 (待接听时使用）
  set_videoCallInfo (state, { isInviter, oppositeID }) {
    state.videoCallInfo.isInviter = isInviter
    state.videoCallInfo.oppositeID = oppositeID
  },
  // 设置当前视频通话msgID
  set_videoCallMsgID (state, msgID) {
    state.videoCallMsgID = msgID
  }
}

const actions = {
  // 获取文件列表
  getFileList ({ commit }) {
    return new Promise((resolve, reject) => {
      fileList('', 1, 1000).then(res => {
        if (res.data.data && res.data.data.data) {
          res.data.data.data.forEach(item => {
            item.file_name = decodeURI(item.file_name)
          })
        }
        if (res.data.data.data !== null) {
          commit('set_fileList', res.data.data.data)
        }
        resolve(res)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
